﻿package com.gengine.resource.mutiThread
{
    import com.gengine.global.*;
    import com.gengine.resource.loader.thread.*;
    import com.gengine.resource.mutiThread.vo.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class ThreadManager extends Object implements IThreadManager
    {
        private const callBackMap:Dictionary;
        private var _id:int = 0;
        private var _initComplete:Boolean = false;
        protected var mainToSub:Object;
        protected var subToMain:Object;
        private var _curShareId:int = 0;
        private var _bgWorker:Object;
        private var messagePool:Array;
        static var _instance:ThreadManager;

        public function ThreadManager()
        {
            this.callBackMap = new Dictionary(false);
            this.messagePool = new Array();
            return;
        }// end function

        public function init() : void
        {
            if (this._initComplete)
            {
                Global.threadManager = this;
            }
            return;
        }// end function

        protected function initRegistClass() : void
        {
            registerClassAlias("ImageLoadVo", ImageLoadVo);
            registerClassAlias("ThreadLoaderObj", ThreadLoaderObj);
            return;
        }// end function

        public function initBgWorker(param1:ByteArray, param2:Object) : void
        {
            this.initRegistClass();
            var _loc_3:* = getDefinitionByName("flash.system.WorkerDomain");
            this._bgWorker = _loc_3.current.createWorker(param1);
            this.mainToSub = param2.current.createMessageChannel(this._bgWorker);
            this.subToMain = this._bgWorker.createMessageChannel(param2.current);
            this._bgWorker.setSharedProperty("mainToSub", this.mainToSub);
            this._bgWorker.setSharedProperty("subToMain", this.subToMain);
            this.subToMain.addEventListener("channelMessage", this.onReceiveSubMessage);
            this._bgWorker.start();
            this.sendMessageToSub({transType:ThreadTransType.StartSecondThreadTest}, this.testThreadHander);
            return;
        }// end function

        private function testThreadHander(param1:Object) : void
        {
            this._initComplete = true;
            return;
        }// end function

        protected function onReceiveSubMessage(event:Event) : void
        {
            var _loc_4:Function = null;
            var _loc_2:* = this.subToMain.receive();
            var _loc_3:* = _loc_2.callBackId;
            if (_loc_3 > 0)
            {
                _loc_4 = this.callBackMap[_loc_3];
                delete this.callBackMap[_loc_3];
                if (_loc_4 != null)
                {
                    this._loc_4(_loc_2);
                }
            }
            return;
        }// end function

        public function sendMessageToSub(param1:Object, param2:Function) : void
        {
            if (param2 != null)
            {
                var _loc_3:String = this;
                var _loc_4:* = this._id + 1;
                _loc_3._id = _loc_4;
                this.callBackMap[this._id] = param2;
                param1.callBackId = this._id;
                if (this._id > 1000000)
                {
                    this._id = 0;
                }
            }
            else
            {
                param1.callBackId = 0;
            }
            this.mainToSub.send(param1);
            return;
        }// end function

        public function bgWokerFirstRun(param1:Stage) : void
        {
            this.initRegistClass();
            var _loc_2:* = getDefinitionByName("flash.system.Worker");
            this.mainToSub = _loc_2.current.getSharedProperty("mainToSub");
            this.subToMain = _loc_2.current.getSharedProperty("subToMain");
            this.mainToSub.addEventListener("channelMessage", this.onReceiveMainMessage);
            param1.addEventListener(Event.ENTER_FRAME, this.enterFrameHander);
            param1.frameRate = 12;
            return;
        }// end function

        private function enterFrameHander(event:Event) : void
        {
            var _loc_2:Object = null;
            if (this.messagePool.length > 0)
            {
                _loc_2 = this.messagePool.shift();
                this.subToMain.send(_loc_2);
            }
            return;
        }// end function

        private function onReceiveMainMessage(event:Event) : void
        {
            var _loc_4:Function = null;
            var _loc_2:* = this.mainToSub.receive();
            var _loc_3:* = _loc_2.subCallBackId;
            if (_loc_3 < 0)
            {
                _loc_4 = this.callBackMap[_loc_3];
                delete this.callBackMap[_loc_3];
                if (_loc_4 != null)
                {
                    this._loc_4(_loc_2);
                }
            }
            else
            {
                this.handleMainMessage(_loc_2);
            }
            return;
        }// end function

        protected function handleMainMessage(param1:Object) : void
        {
            return;
        }// end function

        public function sendMessageToMain(param1:Object, param2:Function, param3:Boolean) : void
        {
            if (param2 != null)
            {
                var _loc_4:String = this;
                var _loc_5:* = this._id - 1;
                _loc_4._id = _loc_5;
                this.callBackMap[this._id] = param2;
                param1.subCallBackId = this._id;
                if (this._id < -1000000)
                {
                    this._id = 0;
                }
            }
            else
            {
                param1.subCallBackId = 0;
            }
            if (param3)
            {
                this.subToMain.send(param1);
            }
            else
            {
                this.messagePool.push(param1);
            }
            return;
        }// end function

        public static function get instance() : ThreadManager
        {
            if (_instance == null)
            {
                trace("调用旧的threadmanager");
            }
            return _instance;
        }// end function

    }
}
